﻿using AutoMapper;
using ERPSystem.Common.Domains.CustomerProfileCommon;
using ERPSystem.Common.Domains.Material;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using SqlSugar;

namespace BasicInformationWrite.API.BasicInformationCommons.Materialss
{
    public class MaterialssCreateHandler:IRequestHandler<MaterialssCreateCommand,Result>
    {
        private readonly IBaseRepository<MaterialInformation> materialInformationRepository;
        private readonly IMapper _mapper;

        public MaterialssCreateHandler(IBaseRepository<MaterialInformation> materialInformationRepository, IMapper mapper)
        {
            this.materialInformationRepository = materialInformationRepository;
            this._mapper = mapper;
        }



        /// <summary>
        /// 新增物料基本档案
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(MaterialssCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                MaterialInformation materialInformation = _mapper.Map<MaterialInformation>(request);//物料基本档 
                materialInformation.CreatedOwnerId = "张三";
                materialInformation.CreatedTime = DateTime.Now;
                var res =  await materialInformationRepository.Add(materialInformation);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
